﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace _2011DBProject
{
    public partial class Guild : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                if (Session["USER"] == null || Session["SERVER"] == null || Session["Ch"] == null || Session["CHARACTER_NAME"] == null)
                {
                    Response.Redirect("default.aspx?isNoSession=1");
                }
                else
                {
                    DataSet1.GUILD_MEMBERDataTable dsg = new DataSet1.GUILD_MEMBERDataTable();
                    DataSet1TableAdapters.GUILD_MEMBERTableAdapter dsga = new DataSet1TableAdapters.GUILD_MEMBERTableAdapter();
                    dsga.Connection.ConnectionString = Settings.strConnectionString;

                    if (dsga.FillBycharacter_name_server(dsg, Settings.strCharacterName, Convert.ToDecimal(Settings.decServer)) < 1)
                        lblGuildYN.Visible = true;

                    btnKick.Attributes.Add("onClick", "return confirm('선택한 회원을 추방(또는 길드를 폐쇄)하시겠습니까?');");
                    btnJoinGuild0.Attributes.Add("onClick", "return confirm('현재 길드에서 탈퇴 하시겠습니까?');");
                    btnJoinGuild.Attributes.Add("onClick", "return confirm('선택한 길드에 가입 하시겠습니까?');");

                    DataSet1TableAdapters.GUILD_MEMBERTableAdapter taGM = new DataSet1TableAdapters.GUILD_MEMBERTableAdapter();
                    taGM.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.GUILD_MEMBERDataTable dtGM = taGM.GetDataBycharacter_name_server(Settings.strCharacterName, Settings.decServer);

                    if(taGM.FillBycharacter_name_server(dtGM, Settings.strCharacterName,Settings.decServer) > 0)
                    {
                        DataSet1.GUILD_MEMBERRow r = dtGM[0];
                        if (r.MEMBER_GRADE == "마스터")
                            gvGuildMember.Visible = true;
                        else
                            gvGuildMember.Visible = false;
                    }
                    else
                        gvGuildMember.Visible = false;
                }
            }
            catch (Exception)
            {
                
                throw;
            }

        }

        protected void btnMakeGuild_Click(object sender, EventArgs e)
        {

            Response.Redirect("MakeGuild.aspx");
        }

        protected void btnJoinGuild_Click(object sender, EventArgs e)
        {
            try
            {
                if (lblGuildList.Text != "선택된 길드가 없습니다.")
                {
                    DataSet1TableAdapters.GUILD_MEMBER1TableAdapter taGm = new DataSet1TableAdapters.GUILD_MEMBER1TableAdapter();
                    taGm.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.GUILD_MEMBER1DataTable gm = taGm.GetDataByServerNoCharacterNameGuildName(Settings.decServer, Settings.strCharacterName, (string)lblGuildList.Text);
                    if (gm.Count == 0)
                    {
                        DataSet1.GUILD_MEMBER1Row row = gm.NewGUILD_MEMBER1Row();
                        row.SERVER_NO = Settings.decServer;
                        row.CHARACTER_NAME = Settings.strCharacterName;
                        row.GUILD_NAME = lblGuildList.Text;
                        row.MEMBER_GRADE = "참가대기자";
                        gm.AddGUILD_MEMBER1Row(row);
                        taGm.Update(gm);
                        sqldsGuild.DataBind();
                        gvGuild.DataBind();
                        Session.Add("GUILD_NAME", lblGuildList.Text);
                        Settings.strGuildName = lblGuildList.Text;
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        protected void gvGuildList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblGuildList.Text = gvGuildList.DataKeys[0].Value.ToString();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        protected void gvGuild_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    lblGuildJoinedList.Text = gvGuild.DataKeys[0].Value.ToString();
            //}
            //catch (Exception)
            //{
                
            //    throw;
            //}


        }

        protected void btnExitGuild(object sender, EventArgs e)
        {
            try
            {
                DataSet1TableAdapters.GUILD_MEMBER1TableAdapter taGm = new DataSet1TableAdapters.GUILD_MEMBER1TableAdapter();
                    taGm.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.GUILD_MEMBER1DataTable gm = taGm.GetDataByServerNoCharacterNameGuildName(Settings.decServer, Settings.strCharacterName, Settings.strGuildName);
                    if (gm.Count > 0)
                    {
                        gm[0].Delete();
                        taGm.Update(gm);
                        sqldsGuild.DataBind();
                        gvGuild.DataBind();
                        Settings.strGuildName = "";
                    }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        protected void btnGoLogout_onclick(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Default.aspx");
        }
        protected void btnGoServer_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Server.aspx");
        }
        protected void btnGoCharacter_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Character.aspx");
        }
        protected void btnGoChannel_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Channel.aspx");
        }
        protected void btnGoMain_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Gameroom.aspx");
        }

        protected void gvGuildMember_SelectedIndexChanged(object sender, EventArgs e)
        {
            try{
                DataSet1TableAdapters.GUILD_MEMBERTableAdapter taGM = new DataSet1TableAdapters.GUILD_MEMBERTableAdapter();
                taGM.Connection.ConnectionString = Settings.strConnectionString;
                DataSet1.GUILD_MEMBERDataTable dtGM = taGM.GetDataBycharacter_name_server(Settings.strCharacterName, Settings.decServer);
                DataSet1.GUILD_MEMBERRow r = dtGM[0];

                if (r.MEMBER_GRADE == "마스터")
                {
                    if ((gvGuildMember.SelectedDataKey[0]).ToString() == Settings.strCharacterName)
                    {
                        DataSet1TableAdapters.GUILD_MEMBERTableAdapter taGM2 = new DataSet1TableAdapters.GUILD_MEMBERTableAdapter();
                        taGM2.Connection.ConnectionString = Settings.strConnectionString;
                        if (taGM2.ScalarQueryGuildMemberCount(Settings.decServer, Settings.strGuildName) > 1)
                        {
                            lblisMaster.Visible = true;
                            btnKick.Visible = false;
                        }
                        else
                        {
                            lblisMaster.Visible = false;
                            btnKick.Text = "길드에서 탈퇴하고 길드를 폐쇄하시겠습니까?";
                            btnKick.Visible = true;
                        }
                    }
                    else
                    {
                        btnKick.Text = "선택회원 추방";
                        lblisMaster.Visible = false;
                        btnKick.Visible = true;
                    }
                }
                else
                {
                    string script = "<script>alert('회원관리는 길드마스터만 할 수 있습니다.');</script>";
                    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);
                }
            }catch{
                string script = "<script>alert('에러번호 - #08 : 관리자에게 문의하세요.');</script>";
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);
            }
        }

        protected void btnKick_Click(object sender, EventArgs e)
        {
            try
            {
                if ((string)gvGuildMember.SelectedDataKey[0] == (string)Session["CHARACTER_NAME"])  //길드마스터이면
                {
                    //마스터 탈퇴시키고
                    DataSet1TableAdapters.GUILD_MEMBERTableAdapter taGM = new DataSet1TableAdapters.GUILD_MEMBERTableAdapter();
                    taGM.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.GUILD_MEMBERDataTable dtGM = taGM.GetDataBycharacter_name_server((string)gvGuildMember.SelectedDataKey[0], Settings.decServer);
                    DataSet1.GUILD_MEMBERRow r = dtGM[0];
                    r.Delete();
                    taGM.Update(dtGM);


                    //길드 삭제하고
                    DataSet1TableAdapters.GUILDTableAdapter taG = new DataSet1TableAdapters.GUILDTableAdapter();
                    taG.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.GUILDDataTable dtG = taG.GetDataBy1ServerNoGuildName(Settings.decServer, Settings.strGuildName);
                    DataSet1.GUILDRow gr = dtG[0];
                    gr.Delete();
                    taG.Update(dtG);

                    //공탁금 돌려주고
                    DataSet1TableAdapters.CHARACTERTableAdapter taCharacter = new DataSet1TableAdapters.CHARACTERTableAdapter();
                    taCharacter.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.CHARACTERDataTable dtCharacter = taCharacter.GetDataByServerNoCharacterName(Settings.decServer, Settings.strCharacterName);
                    DataSet1.CHARACTERRow row = dtCharacter[0];
                    row.NG_AMOUNT += 100000000;
                    taCharacter.Update(dtCharacter);

                    //데이터 다시 뿌리고
                    gvGuildMember.DataBind();
                    gvGuildList.DataBind();
                    Session.Add("GUILD_NAME", null);
                    btnKick.Visible = false;
                }
                else       //일반회원이면 탈퇴시키고
                {
                    DataSet1TableAdapters.GUILD_MEMBERTableAdapter taGM = new DataSet1TableAdapters.GUILD_MEMBERTableAdapter();
                    taGM.Connection.ConnectionString = Settings.strConnectionString;
                    DataSet1.GUILD_MEMBERDataTable dtGM = taGM.GetDataBycharacter_name_server((string)gvGuildMember.SelectedDataKey[0], Settings.decServer);
                    DataSet1.GUILD_MEMBERRow r = dtGM[0];
                    r.Delete();
                    taGM.Update(dtGM);
                    gvGuildMember.DataBind();
                    gvGuildList.DataBind();

                    btnKick.Visible = false;
                }
            }catch{
                string script = "<script>alert('에러번호 - #08 : 관리자에게 문의하세요.');</script>";
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);

            }
        }
    }
}
